//
// Created by lenovo on 2024/1/12.
//

#include "DoubleConverter.h"
#include <set>
std::set<char> Nums ={
        '1',
        '2',
        '3',
        '4',
        '5',
        '6',
        '7',
        '8',
        '9',
        '0'
};

//#include <iostream>
//using namespace std ;

void DoubleConverter::convert() {
    for (auto word : this->src){
        if (Nums.find(word) == Nums.end()){
            if (word == '.'){
                if (this->_above){
                    this->_above = false ;
                    continue;
                }
                else
                    throw std::logic_error("too much '.' in ONE number");
            }
            else{
                throw std::logic_error("Unexpected symbol in a NUMBER");
            }
        }
//        cout <<(int)( word - '0') << ' ';
        if (this->_above){
//            cout << "Above 0" << endl ;
            this->_Int = this->_Int*10 + word - '0';
        }
        else{
//            cout << "Below 0" << endl ;
            this->_Float += (word - '0')*this->weight ;
            this->weight /= 10 ;
        }
    }
    this->_result = this->_Int + this->_Float ;
}

double DoubleConverter::result() {
    if (not _done)
        this->convert();
    _done = true ;

    return _result;
}
